import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import Cookies from 'js-cookie'

const mainRoutes = [{
  path: '/layout/userManage',
  name: 'userManage',
  component: () => import(/* webpackChunkName: "userManage" */ '../views/user-manage.vue'),
  routeName: '用户管理'
}, {
  path: '/layout/tableManage',
  name: 'tableManage',
  component: () => import(/* webpackChunkName: "tableManage" */ '../views/table-manage.vue'),
  routeName: '数据库管理'
}, {
  path: '/layout/customFormDesign',
  name: 'customFormDesign',
  component: () => import(/* webpackChunkName: "customFormDesign" */ '../views/custom-form-design.vue'),
  routeName: '表单设计',
  hidden: true
}, {
  path: '/layout/customFormSubmitDetail',
  name: 'customFormSubmitDetail',
  component: () => import(/* webpackChunkName: "customFormSubmitDetail" */ '../views/custom-form-submit-detail.vue'),
  routeName: '表单填报',
  hidden: true
}, {
  path: '/layout/customFormManage',
  name: 'customFormManage',
  component: () => import(/* webpackChunkName: "customFormManage" */ '../views/custom-form-manage.vue'),
  routeName: '表单管理'
}, {
  path: '/layout/customFormRecord',
  name: 'customFormRecord',
  component: () => import(/* webpackChunkName: "customFormRecord" */ '../views/custom-form-record.vue'),
  routeName: '表单记录'
}, {
  path: '/layout/flowManage',
  name: 'flowManage',
  component: () => import(/* webpackChunkName: "flowManage" */ '../views/flow-manage.vue'),
  routeName: '流程管理'
}, {
  path: '/layout/flowDesign',
  name: 'flowDesign',
  component: () => import(/* webpackChunkName: "flowDesign" */ '../views/flow-design.vue'),
  routeName: '流程设计',
  hidden: true
}, {
  path: '/layout/deptManage',
  name: 'deptManage',
  component: () => import(/* webpackChunkName: "deptManage" */ '../views/dept-manage.vue'),
  routeName: '部门管理'
}, {
  path: '/layout/matterManage',
  name: 'matterManage',
  component: () => import(/* webpackChunkName: "matterManage" */ '../views/matter-manage.vue'),
  routeName: '事项管理'
}, {
  path: '/layout/matterSubmitManage',
  name: 'matterSubmitManage',
  component: () => import(/* webpackChunkName: "matterSubmitManage" */ '../views/matter-submit-manage.vue'),
  routeName: '事项办理'
}, {
  path: '/layout/matterTransact',
  name: 'matterTransact',
  component: () => import(/* webpackChunkName: "matterTransact" */ '../views/matter-transact.vue'),
  routeName: '事项办理',
  hidden: true
}, {
  path: '/layout/matterTransactDetail',
  name: 'matterTransactDetail',
  component: () => import(/* webpackChunkName: "matterTransactDetail" */ '../views/matter-transact-detail.vue'),
  routeName: '事项办理',
  hidden: true
},]

const routes: Array<RouteRecordRaw> = [
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "login" */ '../views/login.vue'),
  },
  {
    path: '/layout',
    name: 'layout',
    component: () => import(/* webpackChunkName: "layout" */ '../views/layout.vue'),
    children: mainRoutes
  },
  {
    path: '/:pathMatch(.*)',
    name: '404',
    component: () => import(/* webpackChunkName: "flowManage" */ '../views/404.vue'),
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});


router.beforeEach((to, from, next) => {
  const token = Cookies.get('token');
  if (token || to.path === '/login') {
    next()
  } else {
    next('/login')
  }
})
export { mainRoutes };
export default router;
